package id.brian;

import id.brian.R;
import id.brian.Panel;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FalldownActivity extends Activity implements SensorEventListener{
	/** Called when the activity is first created. */
	private Panel mPanel;
	private SensorManager mSensorManager;
	private Sensor mAccelerometer;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);
		mPanel = new Panel(this);
		((LinearLayout)findViewById(R.id.LinearLayout1)).addView(mPanel);
		mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		mSensorManager.registerListener(this, mAccelerometer,SensorManager.SENSOR_DELAY_NORMAL);
	}
	
	protected void onResume() {
		super.onResume();
		mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
	}
	
	protected void onPause() {
		super.onPause();
		mSensorManager.unregisterListener(this);
	}

	public void updateScores() {
		runOnUiThread(new Runnable() {
			public void run() {
				TextView tv = (TextView) findViewById(R.id.textView1);
				tv.setTextColor(Color.WHITE);
				((TextView)findViewById(R.id.textView1)).setText("You Lose");
				TextView score = (TextView) findViewById(R.id.textView2);
				score.setTextColor(Color.WHITE);
				((TextView)findViewById(R.id.textView2)).setText("");
			}
		});
	}
	
	public void Score(final int scoreCount) {
		runOnUiThread(new Runnable() {
			public void run() {
				TextView score = (TextView) findViewById(R.id.textView2);
				score.setTextColor(Color.WHITE);
				((TextView)findViewById(R.id.textView2)).setText("SCORE: "+scoreCount);
			}
		});
	}
	
	public void resetScores() {
		runOnUiThread(new Runnable() {
			public void run() {
				TextView tv = (TextView) findViewById(R.id.textView1);
				tv.setTextColor(Color.BLACK);
				((TextView)findViewById(R.id.textView1)).setText("");
			}
		});
	}

	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		//nothing
	}

	public void onSensorChanged(SensorEvent event) {
		if (event.values[0] < 0) mPanel.setBallSpeed(1);
		else if (event.values[0] > 0) mPanel.setBallSpeed(-1);
	}
}